<!---
	Copyright 2010 Edward Trudeau, Jeremy Battle
    This software is freely available under the MIT License: http://www.opensource.org/licenses/mit-license.php
    http://ayc.googlecode.com
    
--->

/**
* I am an Abstract Base class for all Persistant Entities
* @output false
*/
component
{

  /**
  * returns an array of properties that make up the identifier
  * @output false
  */
  public array function getIdentifierColumnNames()
  {
    return ORMGetSessionFactory().getClassMetadata( getClassName() ).getIdentifierColumnNames() ;
  }
  /* broken function...needs work */
  /*public function getColumnPropertyName(columnName){
  	for(b=1;b lte arrayLen(getPersistedProperties()); b++){
		if(this.getColumnName(getPersistedProperties()[b]) EQ columnName){
			writeDump(this.getColumnName(getPersistedProperties()[b]) & columnName & b );
			writeDump(getPersistedProperties());
			abort;
			return this.getPersistedProperties()[b];
		}
	}
  }*/
  public string function getORMType(propertyName)
  {
  	return ORMGetSessionFactory().getAllClassMetadata()[ getClassName() ].getPropertyType(propertyName).getName();
  }
  
  public function getColumnName(propertyName){
  	return ORMGetSessionFactory().getAllClassMetadata()[ getClassName() ].getPropertyColumnNames(propertyName)[1];
  }
  
  public function getMeta(){
  	return ORMGetSessionFactory().getAllClassMetadata()[ getClassName() ];
  }
  
  public function get(propertyName){
  	return evaluate("this.get#propertyName#()");
  }
 
  public function getFCData(propertyName){
  	var fcDataArray = '';
	
	if(structKeyExists(this,"FCData") AND structKeyExists(this.FCData,"propertyName")){
		return this.FCData[propertyName];
	}
	else{
		fcDataArray = request.FS.getFields(objectName=getEntityName(),fieldName=replace(replace(getColumnName(propertyName),"[",""),"]",""));
		
		if(arrayLen(fcDataArray)){
			this.FCData[propertyName] = fcDataArray[1];
			return this.FCData[propertyName];
		}
		else {	
			return getColumnName(propertyName);
		}
	
	}
	
  }
 
  /**
  * returns the name of the Entity
  * @output false
  */
  public string function getEntityName()
  {
    return ORMGetSessionFactory().getClassMetadata( getClassName() ).getEntityName();
  }
  
   public numeric function count()
	{
  	 return ormexecutequery("select count(*) from #getEntityName()#",true);
	}  
  /**
  * returns an array of persisted properties
  * @output false
  */
  public array function getPersistedProperties()
  {
    return ORMGetSessionFactory().getAllClassMetadata()[ getClassName() ].getPropertyNames();
  }
  
  public function getPersistedPropertiesNoRelationships(){
  	return false; 
  }

  /**
  * returns the subclass class name of this object
  * @output false
  */
  public string function getClassName()
  {
    return ListLast( GetMetaData( this ).fullname, "." );
  }
  

  /**
  * returns the value of all persisted entites for debugging only!
  * @output false
  */
  public struct function getMemento() 
  {
    var i = 0;
    var result = {};
    var propertyname = "";
    var propertyvalue = "";
    for ( i=1; i<=ArrayLen( getPersistedProperties() ); i++ )
    {
      propertyname = getPersistedProperties()[ i ];
      //propertyvalue = variables[ propertyname ];
      if ( StructKeyExists( variables, propertyname ) )
      {
        if ( IsSimpleValue( variables[ propertyname ] ) )
        {
          result[ propertyname ] = variables[ propertyname ];
        }
        else if ( IsObject(  variables[ propertyname ] ) )
        {
          result[ propertyname ] = "[object : fullname=" & GetMetaData( variables[ propertyname ] ).fullname & "]";
        }
        else if ( IsArray(  variables[ propertyname ] ) )
        {
          result[ propertyname ] = "[array : size=" & ArrayLen( variables[ propertyname ] ) & "]";
        }
        else if ( IsStruct(  variables[ propertyname ] ) )
        {
          result[ propertyname ] = "[struct : keys=" & StructKeyList( variables[ propertyname ] ) & "]";
        }
        else 
        {
          result[ propertyname ] = "[complex]";
        }
      }
      else
      {
        result[ propertyname ] = "[null]";
      }
    }
    return result;
  }
}

